package com.oymn.myapplication.page;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;
import com.oymn.myapplication.R;
import com.oymn.myapplication.adapter.ViewPagerAdapter;

public class shouye extends AppCompatActivity {

    private Button button;
    private MenuItem lastItem;   // 上一个选中的item
    private ViewPager viewPager;
    String user_phone = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cebianlan);

        Intent intent = getIntent();
        user_phone = intent.getStringExtra("user_phone");

        initView();
        cebianlan();
    }

    public void cebianlan(){
        NavigationView navView = (NavigationView)findViewById(R.id.nav_view);
        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.nav_dingdan:
                        Toast.makeText(shouye.this, "我的订单开发中", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.nav_kefu:
                        Toast.makeText(shouye.this, "客服电话：110", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.nav_shezhi:
                        Toast.makeText(shouye.this, "目前版本不支持设置", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.nav_shoucang:
                        Toast.makeText(shouye.this, "有购物车还不够？？？？？？", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.nav_xiaoxi:
                        Toast.makeText(shouye.this, "好可怜，没人给你发消息", Toast.LENGTH_SHORT).show();
                        break;
                }
                return false;
            }

        });
    }

    public void initView(){
        viewPager=findViewById(R.id.viewpager);
        final BottomNavigationView view=(BottomNavigationView)findViewById(R.id.bottom_navigation);
        //默认 >3 的选中效果会影响ViewPager的滑动切换时的效果，故利用反射去掉
        //BottomNavigationViewHelper.disableShiftMode(view);
        //拿到默认选中的item
        lastItem=view.getMenu().getItem(0);
        //点击选择item
        view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.item_first:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.item_add:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.item_message:
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.item_cart:
                        viewPager.setCurrentItem(3);
                        break;
                }
                return false;
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                if (lastItem != null) {
                    lastItem.setChecked(false);
                } else {
                    view.getMenu().getItem(0).setChecked(false);
                }
                lastItem= view.getMenu().getItem(position);
                lastItem.setChecked(true);
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        setupViewPager(viewPager);
    }
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        adapter.addFragment(new FirstFragment(user_phone));
        adapter.addFragment(new AddFragment());
        adapter.addFragment(new MessageFragment());
        adapter.addFragment(new CartFragment(user_phone));
        viewPager.setAdapter(adapter);
    }
}